<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,user-scalable=no,minimal-ui">
        <meta name="description" content="">
        <meta name="author" content="">
        <link rel="icon" href="../../img/wximg/asset-favico.ico">
        <title>登录</title>
        <link rel="stylesheet" href="../../css/wxcss/page-health-login.css" />
        <link rel="stylesheet" href="../../plugins/wxplugins/elementui/index.css" />
        <script src="../../plugins/wxplugins/jquery/dist/jquery.min.js"></script>
        <script src="../../plugins/wxplugins/healthmobile.js"></script>
        <script src="../../plugins/wxplugins/vue/vue.js"></script>
        <script src="../../plugins/wxplugins/vue/axios-0.18.0.js"></script>
        <script src="../../plugins/wxplugins/elementui/index.js"></script>
    </head>
    <body data-spy="scroll" data-target="#myNavbar" data-offset="150">
    <div class="app" id="app">
        <!-- 页面头部 -->
        <div class="top-header">
            <span class="f-left"><i class="icon-back"></i></span>
            <span class="center">传智健康</span>
            <span class="f-right"><i class="icon-more"></i></span>
        </div>
        <div style="margin-left: 20px">邮箱快速登录</div>
        <!-- 页面内容 -->
        <div class="contentBox">
            <div class="login">
                <form id='login-form'>
                    <div class="input-row">
                        <label>邮箱</label>
                        <div class="loginInput">
                            <input v-model="loginInfo.email" id='account' type="text" placeholder="请输入邮箱号">
                            <input id="validateCodeButton" @click="sendValidateCode()" type="button"
                                   style="font-size: 12px; background-color: #00bbff; color: white; border: none; border-radius: 5px; padding: 5px 10px; cursor: pointer;"
                                   value="获取验证码">
                        </div>
                    </div>
                    <div class="input-row">
                        <label>验证码</label>
                        <div class="loginInput">
                            <input v-model="loginInfo.code" style="width:80%" id='password' type="password" placeholder="请输入验证码">
                        </div>
                    </div>
                    <div class="input-row" style="font-size: 12px">
                        <input type="checkbox" checked>
                        阅读并同意《传智健康用户协议》《传智健康隐私条款》
                    </div>
<!--                    <div class="btn yes-btn"><a @click="login()" href="#">登录</a></div>-->
                    <el-button class="btn yes-btn" type="primary" @click="login()" style="width: 680px;">登录</el-button>
                </form>
            </div>
        </div>
    </div>
    </body>
    <script>
        var vue = new Vue({
            el:'#app',
            data:{
                loginInfo:{
                    email: "3104741394@qq.com",
                    code: ""
                }//登录信息
            },
            methods:{
                checkQQEmail(email) {
                    // QQ 邮箱的正则表达式
                    const qqEmailRegex = /^[a-zA-Z0-9_.-]+@qq\.com$/;
                    return qqEmailRegex.test(email);
                },
                //发送验证码
                sendValidateCode(){
                    var email = this.loginInfo.email;
                    if (!this.checkQQEmail(email)) {
                        this.$message.error('请输入正确的邮箱');
                        return false;
                    }
                    validateCodeButton = $("#validateCodeButton")[0];
                    clock = window.setInterval(doLoop, 1000); //一秒执行一次
                    axios.post("/user/email/send?email=" + email).then((response) => {
                        if(!response.data.flag){
                            //验证码发送失败
                            this.$message.error('验证码发送失败，请检查邮箱输入是否正确');
                        }else{
                            this.loginInfo.code = "";
                        }
                    });
                },
                //登录
                login(){
                    //alert("点击了登录")
                    var email = this.loginInfo.email;
                    if (!this.checkQQEmail(email)) {
                        this.$message.error('请输入正确的邮箱');
                        return false;
                    }
                    axios.post("/user/login/check",this.loginInfo).then((response) => {
                        if(response.data.flag){
                            // 登录成功，存储 JWT 令牌
                            localStorage.setItem('token', response.data.data.token);
                            //登录成功,跳转到index.html
                            window.location.href="index.html";
                        }else{
                            //失败，提示失败信息
                            this.$message.error(response.data.message);
                        }
                    });
                }
            }
        });
    </script>
</html>